//
//  ZJCameraController.m
//  Project
//
//  Created by 郑键 on 17/3/2.
//  Copyright © 2017年 zhengjian. All rights reserved.
//

#import "ZJCameraController.h"
#import <AVFoundation/AVFoundation.h>
#import "ZJCameraMaskView.h"
#import "ZJCamerBorderView.h"

@interface ZJCameraController ()

/**
 多媒体设备
 */
@property (nonatomic, strong) AVCaptureDevice *device;

/**
 输入设备
 */
@property (nonatomic, strong) AVCaptureDeviceInput *input;

/**
 输出
 */
@property (nonatomic, strong) AVCaptureStillImageOutput *output;

/**
 会话
 */
@property (nonatomic, strong) AVCaptureSession *session;

/**
 图像预览层，实时预览图片
 */
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *previewLayer;

/**
 覆盖层视图
 */
@property (nonatomic, strong) ZJCameraMaskView *cameraMaskView;

/**
 bottomFuncView
 */
@property (nonatomic, strong) UIView *bottomShutterFuncView;

/**
 截取区域
 */
@property (nonatomic, assign) CGRect currentImageRect;

/**
 结束回调
 */
@property (nonatomic, copy) CameraCompliteBlock compliteBlock;

@end

@implementation ZJCameraController

#pragma mark - Zero.Const

/**
 *  底部功能按钮栏高度
 */
CGFloat camerControllerBottomFuncViewHeight = 44;

#pragma mark - First.通知

#pragma mark - Second.赋值

#pragma mark - Third.点击事件

/**
 照相按钮点击事件

 @param sender sender
 */
- (void)shutterButtonClick:(UIButton *)sender
{
    AVCaptureConnection *conntion = [self.output connectionWithMediaType:AVMediaTypeVideo];
    if (!conntion) {
        
        return;
    }
    
    __weak ZJCameraController *weakSelf = self;
    [self.output captureStillImageAsynchronouslyFromConnection:conntion completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
        if (imageDataSampleBuffer == nil) {
            return ;
        }
        NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
        
        UIImage *image = [UIImage imageWithData:imageData];
        image = [image ex_fixOrientation];
        UIImage *origal = [image ex_originWithScaleToSize:CGSizeMake(self.view.ex_width, self.view.ex_height * SCREEN_SCALE)];
        UIImage *currentImage = [image ex_imageFromImage:image inRect:self.currentImageRect scale:SCREEN_SCALE];
        
        weakSelf.compliteBlock(origal, currentImage);
    }];
}

#pragma mark - Fourth.代理方法

#pragma mark - Fifth.控制器生命周期

/**
 实例化自定义相机对象
 
 @param compliteBlock       compliteBlock
 @return                    相机
 */
+ (instancetype)cameraControllerCompliteBlock:(CameraCompliteBlock)compliteBlock
{
    ZJCameraController *cameraController        = [[ZJCameraController alloc] init];
    cameraController.compliteBlock              = compliteBlock;
    return cameraController;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    /**
     *  配置界面
     */
    [self setupCustomUI];
    
    /**
     *  开始取景
     */
    [self.session startRunning];
}

#pragma mark - Sixth.界面配置

/**
 配置界面
 */
- (void)setupCustomUI
{
    self.view.backgroundColor           = [UIColor darkGrayColor];
    [self.view insertSubview:self.cameraMaskView atIndex:0];
    [self.view.layer insertSublayer:self.previewLayer atIndex:0];
    [self.view addSubview:self.bottomShutterFuncView];
    [self.bottomShutterFuncView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self.view);
        make.height.mas_equalTo(ADJUST_PERCENT_FLOAT(camerControllerBottomFuncViewHeight));
    }];
}

#pragma mark - Seventh.懒加载

- (UIView *)bottomShutterFuncView
{
    if (!_bottomShutterFuncView) {
        
        /**
         *  初始化配置底部照相功能视图
         */
        _bottomShutterFuncView = [[UIView alloc] init];
        
        /**
         *  照相按钮
         */
        UIButton *shutterButton = [[UIButton alloc] init];
        [shutterButton addTarget:self
                          action:@selector(shutterButtonClick:)
                forControlEvents:UIControlEventTouchUpInside];
        
        [_bottomShutterFuncView addSubview:shutterButton];
        [shutterButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.centerY.mas_equalTo(_bottomShutterFuncView);
            make.width.height.mas_equalTo(_bottomShutterFuncView.mas_height);
        }];
    }
    return _bottomShutterFuncView;
}

- (ZJCameraMaskView *)cameraMaskView
{
    if (!_cameraMaskView) {
        
        /**
         *  配置界面中间空白视图尺寸
         */
        CGFloat borderWidth                         = self.view.bounds.size.width - 15;
        CGFloat borderHeight                        = (borderWidth / 85.6) * 54;
        
        /**
         *  创建borderView
         */
        ZJCamerBorderView *borderView               = [[ZJCamerBorderView alloc] initWithFrame:CGRectMake(0,
                                                                                                          0,
                                                                                                          borderWidth,
                                                                                                          borderHeight)];
        borderView.center                           = self.view.center;
        borderView.tintColor                        = self.navigationController.navigationBar.tintColor;
        
        [self.view addSubview:borderView];
        
        /**
         *  保存裁剪视图范围
         */
        self.currentImageRect                       = borderView.frame;
        
        /**
         *  创建蒙版视图
         */
        _cameraMaskView                             = [ZJCameraMaskView maskViewWithFrame:self.view.bounds
                                                                                 cropRect:borderView.frame];
    }
    return _cameraMaskView;
}

- (AVCaptureVideoPreviewLayer *)previewLayer
{
    if (!_previewLayer) {
        _previewLayer                       = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];
        _previewLayer.frame                 = self.view.bounds;
        _previewLayer.videoGravity          = AVLayerVideoGravityResizeAspectFill;
    }
    return _previewLayer;
}

- (AVCaptureSession *)session
{
    if (!_session) {
        _session                            = [[AVCaptureSession alloc] init];
        _session.sessionPreset              = AVCaptureSessionPreset1280x720;
        
        if ([_session canAddInput:self.input]) {
            [_session addInput:self.input];
        }
        
        if ([_session canAddOutput:self.output]) {
            [_session addOutput:self.output];
        }
    }
    return _session;
}

- (AVCaptureStillImageOutput *)output
{
    if (!_output) {
        _output                             = [[AVCaptureStillImageOutput alloc] init];
    }
    return _output;
}

- (AVCaptureDeviceInput *)input
{
    if (!_input) {
        _input                              = [[AVCaptureDeviceInput alloc] initWithDevice:self.device
                                                                                     error:nil];
    }
    return _input;
}

- (AVCaptureDevice *)device
{
    if (!_device) {
        NSArray *devices                    = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
        [devices enumerateObjectsUsingBlock:^(AVCaptureDevice *  _Nonnull device,
                                              NSUInteger idx,
                                              BOOL * _Nonnull stop) {
            if (device.position == AVCaptureDevicePositionBack) {
                _device                     = device;
                *stop                       = YES;
            }
        }];
    }
    return _device;
}

@end
